﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DAO;

namespace tmp_GUI
{
    public partial class tmp_CuonSachForm : Form
    {
        StringBuilder ma_sach = new StringBuilder();
        public tmp_CuonSachForm()
        {
            InitializeComponent();
        }
        private void ShowMaster(){
           dgvCuonSach.AutoGenerateColumns = false;
            this.dgvCuonSach.DataSource = CuonSachBUS.TimTatCaCuonSach();
        }
        private void tmp_CuonSachForm_Load(object sender, EventArgs e)
        {
            this.cbLoaiSach.DataSource = LoaiSachBUS.TimTatCaLoaiSach();
            this.cbLoaiSach.DisplayMember = "TenLoaiSach";
            this.cbLoaiSach.ValueMember = "MaLoaiSach";
            this.cbTuaDe.DataSource = TuaDeSachBUS.TimTuaDeSachBangLoaiSach(this.cbLoaiSach.SelectedValue.ToString());
            this.cbTuaDe.DisplayMember = "TenTuaDe";
            this.cbTuaDe.ValueMember = "MaTuaDe";

            ShowMaster();

            try
            {
                ma_sach.Append(cbTuaDe.SelectedValue.ToString());
                ma_sach.Append(tbSTT.IntValue.ToString().PadLeft(3, '0'));
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void cbLoaiSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cbTuaDe.DataSource = TuaDeSachBUS.TimTuaDeSachBangLoaiSach(this.cbLoaiSach.SelectedValue.ToString());
            this.cbTuaDe.DisplayMember = "TenTuaDe";
            this.cbTuaDe.ValueMember = "MaTuaDe";
        }

        private void cbMaSach_CheckedChanged(object sender, EventArgs e)
        {
            this.tbMaSach.ReadOnly = !this.cbMaSach.Checked;
            this.tbMaTuaDe.ReadOnly = !this.cbMaSach.Checked;
        }

        private void dgvCuonSach_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dgvCuonSach.CurrentRow != null)
            {
                try
                {
                    this.tbMaSach.Text = dgvCuonSach.SelectedRows[0].Cells[0].Value.ToString();
                    this.tbTinhTrang.Text = dgvCuonSach.SelectedRows[0].Cells[4].Value.ToString();
                    this.tbSTT.Text = dgvCuonSach.SelectedRows[0].Cells[2].Value.ToString();
                    this.tbLanXB.Text = dgvCuonSach.SelectedRows[0].Cells[1].Value.ToString();
                    this.tbMaTuaDe.Text = dgvCuonSach.SelectedRows[0].Cells[3].Value.ToString();

                    this.dtpNgayNhap.Value = DateTime.Parse(dgvCuonSach.SelectedRows[0].Cells[5].Value.ToString());
                    if (dgvCuonSach.SelectedRows[0].Cells[3].Value.ToString() == "YES")
                    {
                        this.cbLent.SelectedItem = "YES";
                    }
                    else this.cbLent.SelectedItem = "NO";
                }
                catch (System.Exception ex)
                {
                	
                }
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                // Check if ma sach exists.
                if (CuonSachDAO.CheckCuonSachBangID(getMaSach()))
                {
                    DialogResult dr = MessageBox.Show(null, "Mã sách đã tồn tại!\nBạn có muốn sửa lại mã sách này?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (dr == DialogResult.OK)
                    {
                        // put update function here
                        myUpdate();
                    }
                }
                else
                    myInsert(ma_sach.ToString());
            }
            catch (System.Exception ex)
            {
            }
        }

        private void myInsert(String masach)
        {
            try
            {
                CuonSachDTO sach = new CuonSachDTO();

                sach.MaSach = masach;
                sach.MaTuaDe = cbTuaDe.SelectedValue.ToString();
                sach.MaLanXB = tbLanXB.ByteValue;
                sach.Lent = cbLent.SelectedItem.ToString();
                sach.NgayNhap = dtpNgayNhap.Value;
                sach.SoThuTu = tbSTT.IntValue;
                sach.TinhTrang = tbTinhTrang.Text;
                if (CuonSachBUS.ThemCuonSach(sach))
                {
                    MessageBox.Show("Insert successful!");
                }
                else
                    MessageBox.Show(null, "Insert unsuccessful!\nPlease check \"ma tua de\" and \"ma lan xuat ban\"", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
                ShowMaster();
            }
            catch (Exception ex)
            {
                label10.Text = "Error: " + ex.Message;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            // check if the masach exists
            if(!CuonSachDAO.CheckCuonSachBangID(tbMaSach.Text)){
                DialogResult dr = MessageBox.Show(null, "Mã sách này chưa tồn tại!\nBạn có muốn thêm vào mã sách này cùng với các thông tin đi kèm?", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    myInsert(tbMaSach.Text);
                }
            }
            else
            {
                myUpdate();
            }
        }

        private void myUpdate()
        {

            CuonSachDTO cuonsach = new CuonSachDTO();
            try
            {
                cuonsach.MaSach = tbMaSach.Text;
                cuonsach.MaTuaDe = tbMaTuaDe.Text;
                cuonsach.MaLanXB = tbLanXB.ByteValue;
                cuonsach.NgayNhap = dtpNgayNhap.Value;
                cuonsach.SoThuTu = tbSTT.IntValue;
                cuonsach.TinhTrang = tbTinhTrang.Text;
                cuonsach.Lent = cbLent.SelectedItem.ToString();
                if (CuonSachBUS.SuaCuonSach(cuonsach))
                {
                    MessageBox.Show("Update successful!");
                }
                else
                    MessageBox.Show(null, "Insert unsuccessful!\nPlease check \"ma tua de\" and \"ma lan xuat ban\"", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                ShowMaster();
            }
            catch (System.Exception ex)
            {
                label10.Text = "Error: " + ex.Message;
            }
        }

        private void cbTuaDe_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbMaTuaDe.Text = cbTuaDe.SelectedValue.ToString();
            try
            {
                string ms = getMaSach();
                tbMaSach.Text = ms;
                FastNavigation(ms);
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private String getMaSach()
        {
            ma_sach.Remove(0, ma_sach.Length);
            ma_sach.Append(cbTuaDe.SelectedValue.ToString());
            ma_sach.Append(tbSTT.IntValue.ToString().PadLeft(2, '0'));
            return ma_sach.ToString();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if(dgvCuonSach.CurrentRow != null){
                myDelete();
            }
        }

        private void myDelete()
        {
            string masach = dgvCuonSach.CurrentRow.Cells[0].Value.ToString();
            if (CuonSachBUS.XoaCuonSach(masach))
            {
                MessageBox.Show("Delete successful!", "Thông báo!");
            }
            else {
                MessageBox.Show(null, "Delete unsuccessful!\nPlease check your \"ma sach\"", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            ShowMaster();
        }
        private void FastNavigation(String masach){
            try{
                
                for (int i = 0; i < dgvCuonSach.Rows.Count; i++)
                {
                    if (dgvCuonSach.Rows[i].Cells[0].Value.ToString().Equals(masach))
                    {
                        dgvCuonSach.Rows[i].Selected = true;
                        return;
                    }
                }
            }
            catch(Exception ex){

            }
        }

        private void tbSTT_TextChanged(object sender, EventArgs e)
        {
            FastNavigation(getMaSach());
            //tbMaSach.Text = getMaSach();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            dgvCuonSach.Rows[0].Selected = true;
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if(dgvCuonSach.CurrentRow != null){
                int index = dgvCuonSach.SelectedRows[0].Index;
                if (index > 0)
                    dgvCuonSach.Rows[index - 1].Selected = true;
            }
        }

        
        private void btnNext_Click(object sender, EventArgs e)
        {
            
            if (dgvCuonSach.CurrentRow != null)
            {                
                int index = dgvCuonSach.SelectedRows[0].Index;
                if (index <= dgvCuonSach.Rows.Count)
                {                 
                    dgvCuonSach.Rows[(index + 1)%(dgvCuonSach.Rows.Count)].Selected = true;                    
                    
                }
                
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            dgvCuonSach.Rows[dgvCuonSach.Rows.Count - 1].Selected = true;
        }

        private void dgvCuonSach_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
